package controllers

import (
	"github.com/astaxie/beego"
)


type TestLoginController struct {
	beego.Controller
}

type User struct {
	Username string
	Password string
}

func (c *TestLoginController) Login() {

	//Cookie
	username := c.Ctx.GetCookie("username")
	password := c.Ctx.GetCookie("password")

	if username != "" && password != ""{
		c.Ctx.WriteString("Username:"+ username+"Password:"+password)
	}else {
		c.Ctx.WriteString(`<html><form action="http://localhost:8081/testLogin" method="post"> 
							<input type="text" name="Username"/>
							<input type="password" name="Password"/>
							<input type="submit" value="提交"/>
					   </form></html>
						`)
	}

	//Session
/*	fmt.Println("1111111")
	username := c.GetSession("username")
	fmt.Println("222222222")
	fmt.Println(username)

	password := c.GetSession("password")

	if name,ok := username.(string) ; ok && name != "" && username!= nil{
		c.Ctx.WriteString("Username:"+ username.(string)+"Password:"+password.(string))
	}else {
		c.Ctx.WriteString(`<html><form action="http://localhost:8081/testLogin" method="post"> 
							<input type="text" name="Username"/>
							<input type="password" name="Password"/>
							<input type="submit" value="提交"/>
					   </form></html>
						`)
	}*/
}

func (c *TestLoginController) Post() {
	u := User{}
	if err := c.ParseForm(&u); err != nil{

	}
	//Cookie
	c.Ctx.SetCookie("username",u.Username, 100, "/")
	c.Ctx.SetCookie("password", u.Password, 100, "/")

	c.Ctx.WriteString("Username:"+ u.Username+"Password:"+u.Password)

	//Session
/*	c.SetSession("username", u.Username)
	c.SetSession("password", u.Password)

	c.Ctx.WriteString("Username:"+ u.Username+"Password:"+u.Password)*/
}
